package com.yonyou.cn.excel;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class Exec {

//    private String pathname;
    private static Workbook workbook;
    private Sheet sheet1;
    static OutputStream stream;


    /**使用例子
     * WriteExcel excel = new WriteExcel("D:\\myexcel.xlsx");
     * excel.write(new String[]{"1","2"}, 0);//在第1行第1个单元格写入1,第一行第二个单元格写入2
     */
    public void write(String[] writeStrings, int rowNumber) throws Exception {
        //将内容写入指定的行号中22222334
        //Row row = sheet1.createRow(rowNumber);
        //遍历整行中的列序号
//         for (int j = 0; j < writeStrings.length; j++) {
//             //根据行指定列坐标j,然后在单元格中写入数据
//             Cell cell = row.createCell(j);
//             cell.setCellValue(writeStrings[j]);
//         }
// //         stream = new FileOutputStream(pathname);
//         workbook.write(stream);

    }

    public Exec(String excelPath) throws Exception {
        //在excelPath中需要指定具体的文件名(需要带上.xls或.xlsx的后缀)
//        this.pathname = excelPath;
        String fileType = excelPath.substring(excelPath.lastIndexOf(".") + 1, excelPath.length());
        //创建文档对象
        if (fileType.equals("xls")) {
            //如果是.xls,就new HSSFWorkbook()
            workbook = new HSSFWorkbook();
        } else if (fileType.equals("xlsx")) {
            //如果是.xlsx,就new XSSFWorkbook()
            workbook = new SXSSFWorkbook();
        } else {
            throw new Exception("文档格式后缀不正确!!！");
        }
        // 创建表sheet
        sheet1 = workbook.createSheet("data");
    }



    public static void main(String[] args) throws Exception{
        String property = System.getProperty("user.dir");
        System.out.println("当前路径："+property);
        /*if(args==null||args.length!=2){
            System.out.println("入参有误，请修改后执行");
            return;
        }*/
        String para1 = args[0];
        double n = Double.valueOf(args[1]);
//        String para1 = "q";
//        double n = 0.5;
        String path = "";
        if(para1.equals("q")){
            path = property+"//取保.xlsx";
            File file = new File(path);
            if(file.exists()){
                file.delete();
            }

            Exec exec = new Exec(path);

            String[] xxx = new String[60];
            for(int x=1;x<=60;x++){
                xxx[x-1] = "L"+x;
            }
            exec.write(xxx,0);

            int count = (int)(n*10000);
            for(int i=0;i<(10000*n)-1;i++){

                String mon = "";
                final double d = Math.random();
                final int ha = (int)(d*13);
                String tem = "";
                if(ha==0){
                    tem = "10";
                }else if(ha<10){
                    tem = "0"+ha;
                }else{
                    tem = ha+"";
                }
                mon = "2019"+tem;


                exec.write(new String[]{mon,"1","222","001",i+".00",i+1+".00",i+2+".00",suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji()},i+1);
                System.out.println("已生成："+(i+1)+"条，剩余："+(count-(i+1))+"条");
            }
        }else if(para1.equals("j")){
            path = property+"//居保.xlsx";
            File file = new File(path);
            if(file.exists()){
                file.delete();
            }

            Exec exec = new Exec(path);

            String[] xxx = new String[24];
            for(int x=1;x<=24;x++){
                xxx[x-1] = "L"+x;
            }
            exec.write(xxx,0);

            int count = (int)(n*10000);
            for(int i=0;i<(10000*n)-1;i++){

                String mon = "";
                final double d = Math.random();
                final int ha = (int)(d*13);
                String tem = "";
                if(ha==0){
                    tem = "10";
                }else if(ha<10){
                    tem = "0"+ha;
                }else{
                    tem = ha+"";
                }
                mon = "2019"+tem;


                exec.write(new String[]{mon,"222","005",i+".00",i+1+".00",i+1+".00",2*i+1+".00",2*i+1+".00",suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),suiji(),"0"},i+1);
                System.out.println("已生成："+(i+1)+"条，剩余："+(count-(i+1))+"条");
            }
        }else{
            System.out.println("参数有误");
            return;
        }

        stream = new FileOutputStream(path);
        workbook.write(stream);
        stream.close();
        System.out.println(10000*n+"条数据生成完成");


    }

    public static String suiji(){
        final double d = Math.random();
        final int ha = (int)(d*100);
        return ha+"";
    }






}
